package com.glacier.yuyuan.gen.webapp.web;

import cn.hutool.core.util.StrUtil;
import com.glacier.common.core.domain.Page;
import com.glacier.common.core.domain.Result;
import com.glacier.common.core.domain.form.ListWrapper;
import com.glacier.yuyuan.gen.webapp.domain.GenTableField;
import com.glacier.yuyuan.gen.webapp.service.GenTableFieldService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * date 2022-11-10 15:12
 * 代码生成表字段 控制层
 *
 * @author glacier
 * @version 1.0
 */
@RequestMapping("/genTableField")
@Controller
public class GenTableFieldController extends BaseController {

    private GenTableFieldService genTableFieldService;

    @Autowired
    public void setGenTableFieldService(GenTableFieldService genTableFieldService) {
        this.genTableFieldService = genTableFieldService;
    }

    @GetMapping("")
    public String list() {
        return "view/genTableField/genTableFieldList";
    }

    @ResponseBody
    @PostMapping("/page")
    public Result<List<GenTableField>> page(Page<GenTableField> page, GenTableField genTableField) {
        page = genTableFieldService.findPage(page, genTableField);
        return Result.ok("ok", page.getData(), page.getTotal());
    }

    @GetMapping("/form")
    public String form(GenTableField genTableField, Model model) {
        if (StrUtil.isNotBlank(genTableField.getId())) {
            genTableField = genTableFieldService.findById(genTableField.getId());
        }
        model.addAttribute("entity", genTableField);
        return "view/genTableField/genTableFieldForm";
    }

    @PostMapping("/save")
    @ResponseBody
    public Result<Integer> save(GenTableField genTableField) {
        return Result.ok("操作成功！", genTableFieldService.save(genTableField));
    }

    @ResponseBody
    @PostMapping("/del")
    public Result<String> del(ListWrapper<String> form) {
        genTableFieldService.batchDelete(form.getDatas());
        return Result.ok("操作成功", "");
    }
}
